﻿using Prism.Mvvm;
using HandyControl.Controls;
using IntoTheVoid.Models;
using Prism.Commands;
using System;
using Microsoft.WindowsAPICodePack.Dialogs;
using HandyControl.Tools.Extension;
using IntoTheVoid.Units;
using System.Windows;
using System.Windows.Threading;
using IntoTheVoid.State;

namespace IntoTheVoid.ViewModels
{
	class InstallPathChangeViewModel : BindableBase
	{
		// 开始安装按钮是否可用
		private bool isSaveDataEnabled = true;
		public bool IsSaveDataEnabled
		{
			get { return isSaveDataEnabled; }
			set { isSaveDataEnabled = value; RaisePropertyChanged(); }
		}
		// 空间不足显示
		private string iIsEnoughShow = "Hidden";
		public string IsEnoughShow
		{
			get { return iIsEnoughShow; }
			set { iIsEnoughShow = value; RaisePropertyChanged(); }
		}
		// 安装路径
		private string iInstallPathText;
		public string InstallPathText
		{
			get { return iInstallPathText; }
			set { iInstallPathText = value; RaisePropertyChanged(); }
		}
		// 资源大小
		private string iResourceSizeText="1G";
		public string ResourceSizeText
		{
			get { return iResourceSizeText; }
			set { iResourceSizeText = value; RaisePropertyChanged(); }
		}
		// 解压需要大小
		private string iResourceUnZipNeedSizeText = "1G";
		public string ResourceUnZipNeedSizeText
		{
			get { return iResourceUnZipNeedSizeText; }
			set { iResourceUnZipNeedSizeText = value; RaisePropertyChanged(); }
		}
		// 可用空间大小
		private string iDiskSizeText = "1G";
		public string DiskSizeText
		{
			get { return iDiskSizeText; }
			set { iDiskSizeText = value; RaisePropertyChanged(); }
		}

		private Visibility showSize = Visibility.Hidden;
		public Visibility ShowSize
		{
			get { return showSize; }
			set
			{
				showSize = value;
				RaisePropertyChanged();
			}
		}
		private BaseInfoContentModel baseInfoContentModel;
		public Dialog ParentDialog { get; set; }

		public DelegateCommand SaveDataCommand { get; private set; }
		public DelegateCommand ChooseInstallPathCommand { get; private set; }

		

		public InstallPathChangeViewModel()
		{
			baseInfoContentModel = BaseInfoContentModel.Instance;
			SaveDataCommand = new DelegateCommand(SaveData);
			ChooseInstallPathCommand = new DelegateCommand(ChoosePath);
			SetInstallPath();
			SetSizeText();
		}

		private void SetSizeText()
		{
			string disk = InstallPathText.Substring(0, InstallPathText.IndexOf(":"));
			long space = Common.GetHardDiskFreeSpace(disk);
			DiskSizeText = Common.FormatByteSizeNum(space);

			var httpDownLoadInfo = GameDownloadConfigModel.Instance.DownLoadInfo;
			if(httpDownLoadInfo.OpenedLink == "" || httpDownLoadInfo.OpenedLink == null)
			{
				return;
			}
			
			var size = DownLoadManager.GetDownLoadFileSize(httpDownLoadInfo.OpenedLink);
			if(size < 0)
			{
				return;
			}
			ShowSize = Visibility.Visible;
			long needSize = size * 2;
			ResourceSizeText = Common.FormatByteNum(size);
			ResourceUnZipNeedSizeText = Common.FormatByteNum(needSize);

			if (space < needSize)
			{
				IsEnoughShow = "Visible";
				IsSaveDataEnabled = false;
			}
			else
			{
				IsEnoughShow = "Hidden";
				IsSaveDataEnabled = true;
			}
		}
		private void SetInstallPath()
		{
			var basePath = AppDomain.CurrentDomain.BaseDirectory;
			var defaultPath = System.IO.Path.Combine(basePath, "Game");
			var path = baseInfoContentModel.GetValue<string>("gameInstallPath", null);
			if(path == "" || path == null)
			{
				// baseInfoContentModel.SetValue("gameInstallPath", defaultPath);
				Common.SaveInstallPath(defaultPath);
				path = defaultPath;
			}
			InstallPathText = path;
		}

		private void SaveData()
		{
			Common.SaveInstallPath(InstallPathText);
			ParentDialog.Close();
		}
		private void ChoosePath()
		{
			var dialog = new CommonOpenFileDialog(InstallPathText);
			dialog.IsFolderPicker = true;
			CommonFileDialogResult result = dialog.ShowDialog();
			if (result == CommonFileDialogResult.Ok)
			{
				var fileName = dialog.FileName;
				InstallPathText = System.IO.Path.Combine(fileName, "IntoTheVoid");
				Common.SaveInstallPath(InstallPathText);
				MainViewModel.stateContext.CurState = new FirstCheckState(MainViewModel.stateContext, true);
				MainViewModel.stateContext.CurState.Start(MainViewModel.stateContext);
				SetSizeText();
			}
		}

	}
}
